package de.homelabs.modules.content.service;

import org.springframework.stereotype.Service;

import de.homelabs.modules.content.domain.HLHTMLPageContent;
import de.homelabs.modules.content.domain.HLPageElement;
import de.homelabs.modules.content.domain.HLPageList;
import de.homelabs.modules.content.domain.HLPluginPageContent;
import de.homelabs.modules.tools.strategy.HLJAXBImportStrategy;
import de.homelabs.modules.tools.strategy.IHLImportStrategy;

@Service("hlPageManager")
public class HLPageManager {

	//Singeltion Design Pattern
	private static HLPageManager instance = new HLPageManager();
		
	//vars
	private IHLImportStrategy<HLPageList> importStrategy = new HLJAXBImportStrategy<HLPageList>(HLPageList.class);
	
	private HLPageList pageList = new HLPageList();
	/**
	 * private constructor
	 */
	private HLPageManager(){}
	
	/**
	 * get instance 
	 * @return
	 */
	public static HLPageManager getInstance(){
		return instance;
	}
	
	/********************
	 * functions
	 */
	public HLPageElement getPageElementByURL(String url){
		//load content
		importPageList();
		
		//find page
		for (HLPageElement pageElement : this.pageList.getPages()){
			if (pageElement.getPageUrl().equalsIgnoreCase(url)){
				//return element
				return pageElement;
			}
		}
		
		//no Element found
		return null;
	}
	
	private boolean importPageList(){
		Class[] regc = new Class[]{HLPageList.class, HLHTMLPageContent.class, HLPluginPageContent.class};
		if (importStrategy instanceof HLJAXBImportStrategy){
			((HLJAXBImportStrategy) importStrategy).setRegisteredClasses(regc);
			this.pageList = importStrategy.importElementList("/hlpages.xml");
		} else {
			this.pageList = importStrategy.importElementList("/hlpages.xml");
		}
		return true;
	}
}
